//
//  UserLoginViewController.m
//  YoucApp
//
//  Created by Liam on 11/19/12.
//
//

#import "UserLoginViewController.h"
#import "UserRegisterViewController.h"
#import "UserManager.h"
#import "UICheckBox.h"

@interface UserLoginViewController ()

@property (retain, nonatomic) IBOutlet UICheckBox *btnAutoLogin;

@end

@implementation UserLoginViewController

@synthesize textEmail = _textEmail;
@synthesize textPassword = _textPassword;
@synthesize btnAutoLogin = _btnAutoLogin;

- (NSNumber *)isValidEmail:(NSString *)checkString
{
	BOOL stricterFilter = YES;
	NSString *stricterFilterString = @"[A-Z0-9a-z._%+-]+@[A-Za-z0-9.-]+\\.[A-Za-z]{2,4}";
	NSString *laxString = @".+@.+\\.[A-Za-z]{2}[A-Za-z]*";
	NSString *emailRegex = stricterFilter ? stricterFilterString : laxString;
	NSPredicate *emailTest = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", emailRegex];
	return [NSNumber numberWithBool:[emailTest evaluateWithObject:checkString]];
}

- (NSNumber *)isValidPassword:(NSString *)password
{
	BOOL bRet = ([password length] >= 6);
	
	return [NSNumber numberWithBool:bRet];
}

- (id)init
{
	self = [self initWithNibName:@"UserLoginViewController" bundle:nil];
	return self;
}

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
	self.title = @"登录";
	
	UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
	UIImage *image = [UIImage imageNamed:@"wy_nav_back_unfocus.png"];
	[button setImage:image forState:UIControlStateNormal];
	[button setImage:[UIImage imageNamed:@"wy_nav_back_focus.png"] forState:UIControlStateHighlighted];
	[button sizeToFit];
	button.frame = CGRectMake(0, 0, image.size.width, button.frame.size.height);
	[button addTarget:self action:@selector(click_back) forControlEvents:UIControlEventTouchUpInside];
	
	UIBarButtonItem *backItem = [[[UIBarButtonItem alloc] initWithCustomView:button] autorelease];
	
	[self.navigationItem setLeftBarButtonItem:backItem];
	
	self.textEmail.text = [[UserManager instance] getSavedLoginName];
}

- (void)click_back
{
	[self dismissModalViewControllerAnimated:YES];
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (IBAction)clickRegister:(id)sender
{
	UserRegisterViewController *registerViewController = [[[UserRegisterViewController alloc] init] autorelease];
	[self.navigationController pushViewController:registerViewController animated:YES];
}

- (IBAction)inputDone
{
	NSString * email = self.textEmail.text;
	NSString * password = self.textPassword.text;
	
	if (![self isValidTextField:self.textEmail checkSelector:@selector(isValidEmail:) errMsg:@"请输入正确的邮箱地址"])
	{
		return;
	}
	
	if (![self isValidTextField:self.textPassword checkSelector:@selector(isValidPassword:) errMsg:@"请输入正确的密码"])
	{
		return;
	}
	
	[[UserManager instance] userLogin:email password:password];
	
	[super inputDone];
}

- (void)dealloc {
	[_textEmail release];
	[_textPassword release];
	[_btnAutoLogin release];
	[super dealloc];
}

- (void)viewDidUnload {
	[self setTextEmail:nil];
	[self setTextPassword:nil];
	[self setBtnAutoLogin:nil];
	[super viewDidUnload];
}

- (NSString *)getPassword
{
	if (self.btnAutoLogin.selected)
		return self.textPassword.text;
	return nil;
}

@end
